package com.hwadee.ems.domain.vo;

import java.time.LocalDate;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

import com.fasterxml.jackson.annotation.JsonIgnore;

import com.hwadee.ems.domain.po.Approval;
import com.hwadee.ems.domain.po.Class;
import com.hwadee.ems.domain.po.Course;
import com.hwadee.ems.domain.po.PaperApplication;
import com.hwadee.ems.domain.po.ScheduledCourse;
import com.hwadee.ems.domain.po.User;
import com.hwadee.ems.enums.ApprovalStatus;
import com.hwadee.ems.service.IApprovalService;
import com.hwadee.ems.service.IClassService;
import com.hwadee.ems.service.ICourseService;
import com.hwadee.ems.service.IScheduledCourseService;
import com.hwadee.ems.service.IUserService;
import com.hwadee.ems.service.impl.ApprovalServiceImpl;
import com.hwadee.ems.service.impl.ClassServiceImpl;
import com.hwadee.ems.service.impl.CourseServiceImpl;
import com.hwadee.ems.service.impl.ScheduledCourseServiceImpl;
import com.hwadee.ems.service.impl.UserServiceImpl;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class PaperApplicationVO {

    @JsonIgnore
    private IScheduledCourseService scheduledCourseService = ScheduledCourseServiceImpl.getInstance();

    @JsonIgnore
    private ICourseService courseService = CourseServiceImpl.getInstance();

    @JsonIgnore
    private IClassService classService = ClassServiceImpl.getInstance();

    @JsonIgnore
    private IUserService userService = UserServiceImpl.getInstance();

    @JsonIgnore
    private IApprovalService approvalService = ApprovalServiceImpl.getInstance();


    public PaperApplicationVO(PaperApplication paperApplication) {
        this.id = paperApplication.getId();
        this.courseId = paperApplication.getCourseId();
        ScheduledCourse scheduledCourse = scheduledCourseService.getById(paperApplication.getCourseId());
        Course course = courseService.getById(this.courseId);
        this.courseName = course.getName();
        Class classEntity = classService.getById(scheduledCourse.getClassId());
        this.classId = classEntity.getId();
        this.className = classEntity.getName();
        this.applicantId = paperApplication.getApplicantId();
        this.applicantName = userService.getById(paperApplication.getApplicantId()).getName();
        this.applicationDate = paperApplication.getApplicationDate();
        this.approvalId = paperApplication.getApprovalId();
        Approval approval = approvalService.getById(this.approvalId);
        this.approverId = approval.getApproverId();
        this.approverName = userService.getOptById(this.approverId).map(User::getName).orElse(null);
        this.approvalStatus = approval.getStatus();
        this.approvalOpinion = approval.getOpinion();
        this.approvalDate = approval.getApprovalDate();
        this.paperContent = paperApplication.getPaperContent();
    }
    
    /**
     * 系部试卷提交申请编号
     */
    private Long id;

    /**
     * 课程编号 references course(id) for (name)
     */
    private Long courseId;

    /**
     * 课程名称
     */
    private String courseName;

    /**
     * 上课班级编号 references class(id) for (name)
     */
    private Long classId;

    /**
     * 上课班级名称
     */
    private String className;

    /**
     * 申请人编号 references user(id) for (name)
     */
    private Long applicantId;

    /**
     * 申请人姓名
     */
    private String applicantName;

    /**
     * 申请日期
     */
    private LocalDate applicationDate;

    /**
     * 审批编号 references approval(id) for (status, opinion, approval_date)
     */
    private Long approvalId;

    /**
     * 审批人编号 references user(id) for (name)
     */
    private Long approverId;

    /**
     * 审批人姓名
     */
    private String approverName;

    /**
     * 审批状态
     */
    private ApprovalStatus approvalStatus;

    /**
     * 审批意见
     */
    private String approvalOpinion;

    /**
     * 审批日期
     */
    private LocalDate approvalDate;

    /**
     * 试卷内容
     */
    private String paperContent;

}
